package selenium;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class 拼接帐号 {
	// 去除下载重复的视频       
	//private static final String baidu = "115.239.211.112";

	public static void main(String[] args) {
		
		
		//getNetworkState(baidu);
		// *********************唯一需要更改的地方********************
		String filepath = "D:\\3712";// E:\分类视频采集王\好看视频\搞笑
		
		String filepath1 = "陶林帐号";//帐号密码
		
		String filepath2 = "陶林帐号cookie";// cookie
		// *********************唯一需要更改的地方********************
		
		List<String> zhanghus = new ArrayList<String>();
		List<String> cookies = new ArrayList<String>();
		List<String> newZhangHaos = new ArrayList<String>();
				//**************读文件****************
				try {
					zhanghus = readFileAll(
							filepath + "/" + filepath1+".txt");
					cookies = readFileAll(
							filepath + "/" + filepath2+".txt");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//**************读文件****************
				for (int i = 0; i < zhanghus.size(); i++) {
					String email = zhanghus.get(i).split("----")[0];
					String cookie = cookies.get(i).split("=")[1];
					String newZhangHao = email+"/"+cookie;
					newZhangHaos.add(newZhangHao);
				}
				try {
					writeFile(filepath + "/" + filepath2+"拼接后.txt", newZhangHaos);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
		
	}
		
		// 方法1：
		public void method1() {
			FileWriter fw = null;
			try {
				// 如果文件存在，则追加内容；如果文件不存在，则创建文件
				File f = new File("E:\\dd.txt");
				fw = new FileWriter(f, true);
			} catch (IOException e) {
				e.printStackTrace();
			}
			PrintWriter pw = new PrintWriter(fw);
			pw.println("追加内容");
			pw.flush();
			try {
				fw.flush();
				pw.close();
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		// 方法2：
		public static void method2(String file, String conent) {
			BufferedWriter out = null;
			try {
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
				out.write(conent + "\r\n");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		// 方法3：
		public static void method3(String fileName, String content) {
			try {
				// 打开一个随机访问文件流，按读写方式
				RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
				// 文件长度，字节数
				long fileLength = randomFile.length();
				// 将写文件指针移到文件尾。
				randomFile.seek(fileLength);
				randomFile.writeBytes(content + "\r\n");
				randomFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public static List<String> readFileAll(String filePath) throws IOException {
			File file = new File(filePath);
			BufferedReader reader = null;
			List<String> list = new ArrayList<String>();
			reader = new BufferedReader(new FileReader(file));
			String text = reader.readLine();
			while (text != null) {
				list.add(text.trim() + "\r\n");
				text = reader.readLine();
			}
			reader.close();
			return list;
		}

		public static List<String> readFile(String filePath, String str) throws IOException {
			File file = new File(filePath);
			BufferedReader reader = null;
			List<String> list = new ArrayList<String>();
			reader = new BufferedReader(new FileReader(file));
			String text = reader.readLine();
			while (text != null) {
				if (!text.trim().equals(str)) {
					list.add(text.trim() + "\r\n");
				}
				text = reader.readLine();
			}
			reader.close();
			return list;
		}

		public static void writeFile(String filePath, List<String> list) throws IOException {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			FileOutputStream outputStream = new FileOutputStream(file);
			for (String s : list) {
				outputStream.write(s.getBytes());
			}
			outputStream.close();
		}
		
		public static void writeFileRN(String filePath, List<String> list) throws IOException {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			FileOutputStream outputStream = new FileOutputStream(file);
			for (String s : list) {
				outputStream.write((s+"\r\n").getBytes());
			}
			outputStream.close();
		}

		private static boolean getNetworkState(String ip) {
			Runtime runtime = Runtime.getRuntime();
			try {
				Process process = runtime.exec("ping " + ip);
				InputStream iStream = process.getInputStream();
				InputStreamReader iSReader = new InputStreamReader(iStream, "UTF-8");
				BufferedReader bReader = new BufferedReader(iSReader);
				String line = null;
				StringBuffer sb = new StringBuffer();
				while ((line = bReader.readLine()) != null) {
					sb.append(line);
				}
				iStream.close();
				iSReader.close();
				bReader.close();
				String result = new String(sb.toString().getBytes("UTF-8"));
				if (!StringUtils.isBlank(result)) {
					if (result.indexOf("TTL") > 0 || result.indexOf("ttl") > 0) {
						return true;//有网
					} else {
						return true;//没有网
					}
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;//没有网
		
		}
}
